<?php
//
// 拼单任务管理
//
class Controller_Admin_TuanTask extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type     = 'tuantask';
        $this->_list_pagesize = 20;
        $this->_no_buttons    = TRUE;

        parent::__construct($req, $res);

        $this->_list_where = array();

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );

        $this->_shopId = App_Shop_Manager::currentShopId();
        if ($this->_shopId > 0)
            $this->_list_where[] = array('shopId', '=', $this->_shopId);
    }

	//首页
    public function action_index ()
    {
        $this->action_list();
    }

    
    public function action_list ($page = 1)
    {
        $filter = array();
        $query  = $this->request->query();

        $filter['startDay'] = Arr::get($query, 'startDay');
        $filter['stopDay']  = Arr::get($query, 'stopDay');
        $filter['orderNo']  = Arr::get($query, 'orderNo');
        $filter['status']   = Arr::get($query, 'status', -1);
        $filter['shopId']   = Arr::get($query, 'shopId');
        $filter['statusList'] = App_Order_Tuan::options_statusList();
        $filter['shopList']   = App_Shop::options_shopNames();

        if ($this->_shopId > 0)
            $filter['shopId'] = $this->_shopId;

        if (empty($filter['startDay']))
            $filter['startDay'] = date("Y-m-d", time() - 86400 * 7);

        if (empty($filter['stopDay']))
            $filter['stopDay'] = date("Y-m-d", time());

        // list_where
        if ($filter['status'] >= 0)
            $this->_list_where[] = array('t.status', '=', $filter['status']);

        if ($filter['shopId'] > 0)
            $this->_list_where[] = array('g.shopId', '=', $filter['shopId']);

        if (! empty($filter['orderNo']))
        {
            //$this->_list_where[] = array('orderNo', 'like', '%'.$filter['orderNo'].'%');
        }

        if (! empty($filter['startDay']))
            $this->_list_where[] = array('t.created', '>=', strtotime($filter['startDay']));

        if (! empty($filter['stopDay']))
            $this->_list_where[] = array('t.created', '<', strtotime($filter['stopDay']) + 86400);

        $this->template->set('form_include_head_file', $this->_form->include_edit_file());
        $this->template->set('filter', $filter);

        parent::action_list($page);
    }

    public function action_update ($nid)
    {
        exit;
    }

	//删除订单
    public function action_delete ($nid)
    {
        exit;
    }

    public function action_detail ($nid)
    {
        $tuan = App_Order_Tuan::detail($nid);
        $this->template->set('tuan', $tuan);

        $this->template->set('title', '团购任务');
        $this->template->set('tuan', $tuan);
        $this->template->set_filename("smarty:admin/order/tuantask");
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

    private function rebuild_post_data (& $post)
    {
        //
    }

    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            //
        }
    }

    public function list_count_data ()
    {
        $n = App_Order_Tuan::taskCount($this->_list_where);
        return $n;
    }

    public function list_get_data ($offset, $limit)
    {
        return App_Order_Tuan::taskList($offset, $limit, $this->_list_where);
    }

    public function actions_by_list_row (& $row, & $menus)
    {
        $status = Arr::get($row, 'status', 0);
        unset($menus[1]);
        unset($menus[2]); 
    }
}
